package controller;

import entity.License;
import entity.User;
import service.User_Acount_Service;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "AdminController",urlPatterns = "*.do")
public class AdminController extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("contentType", "text/html; charset=utf-8");
        int msg = 0;//记录状态 1 = 执行成功  0 = 执行失败
        String action = request.getParameter("action");

        User_Acount_Service uas = new User_Acount_Service();
        User tua = new User();
        switch (action){
            case "AdminLogin":
                String login_id = request.getParameter("loginId");
                String password = request.getParameter("password");
                if(request.getSession().getAttribute("license")!=null) {
                	request.getSession().removeAttribute("license");
                }
                if(login_id != null && password != null){
                    tua.setLoginId(login_id);
                    tua.setPassword(password);
                    boolean isTrue = uas.adminLogin(tua);
                    //登录
                    if(isTrue == true){
                        msg = 1;
                        request.getSession().setAttribute("userName", login_id);
                        request.getSession().setAttribute("userList", uas.shopShow());
                        ArrayList<User> lists = uas.show_shopType("user");
                        request.getSession().setAttribute("licenseList", lists);
                    }else{
                        msg = 0;
                    }
                }else{
                    msg = 0;
                }
                response.getWriter().print(msg);
                break;
            case "shopOperation"://封禁解封处理
                String shopId = request.getParameter("shopId");
                String butVal = request.getParameter("butVal");
                if (shopId != "" && shopId != null) {//验证获取的商户ID是否为空
                    msg = uas.shopOperation(shopId,butVal);
                    request.getSession().setAttribute("userList", uas.shopShow());
                }else{
                    msg = 0;
                }
                response.getWriter().print(msg);
                break;
            case "shopApplyShow"://用户成为商家申请列表
                String shopType = request.getParameter("shopType");
                if (shopType != null && !shopType.equals("")) {//验证获取的查询条件是否为空
                    if(shopType.equals("user")){
                        ArrayList<User> lists = uas.show_shopType(shopType);
                        if(lists != null){
                            msg = 1;
                        }else{
                            msg = -1;
                        }
                    }else{
                        msg = 2;
                    }
                } else {
                    msg = 0;
                }
                response.getWriter().print(msg);
                break;
            case "adminAudit"://查询申请人申请信息
                String userId = request.getParameter("userId");
                if(userId != null && !userId.equals("")){
                    msg = 1;
                    License license = uas.shopApply(userId);
                    System.out.println(license.getEstablishTime());
                    request.getSession().setAttribute("license", license);
                }else{
                    msg = 0;
                }
                response.getWriter().print(msg);
                break;
            case "adminYes_no"://审核
                String yes_no = request.getParameter("yes_no");
                String userId2 = request.getParameter("userId");
                if(yes_no != null && !yes_no.equals("")&& userId2 != null && !userId2.equals("")){
                    msg = uas.apply_yesOrNo(yes_no, Integer.parseInt(userId2));
                }
                response.getWriter().print(msg);
                break;
        }
    }
}
